package com.lt.web;

import com.lt.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取最大的域ServletContext
        ServletContext servletContext = getServletContext();
        //获取应用上下文 去加载xml配置文件创建spring容器 命名为app
        //ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");



        //通过ServletContext 在自定义的监听器中获取app
        //ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        //通过自定义的工具获取spring容器app
        //ApplicationContext app = WebAppContext.getWebAppContextName(servletContext);

        //通过spring配置的监听器获取app
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        UserService userService = (UserService) app.getBean("userService");
        userService.sayHello();
    }
}
